package permission_pb

import (
	"context"
	"errors"
	"github.com/fatih/color"
	"github.com/jinzhu/gorm"
	uuid "github.com/satori/go.uuid"
)

func (m *UserServiceDefaultServer) mustEmbedUnimplementedUserServiceServer() {
	//TODO implement me
	panic("implement me")
}
func (m *AuthorityServiceDefaultServer) mustEmbedUnimplementedAuthorityServiceServer() {
	//TODO implement me
	panic("implement me")
}
func (m *MenuServiceDefaultServer) mustEmbedUnimplementedMenuServiceServer() {
	//TODO implement me
	panic("implement me")
}
func (m *ApiServiceDefaultServer) mustEmbedUnimplementedApiServiceServer() {
	//TODO implement me
	panic("implement me")
}
func (m *UserRltAuthorityServiceDefaultServer) mustEmbedUnimplementedUserRltAuthorityServiceServer() {
	//TODO implement me
	panic("implement me")
}
func (s *UserORM) BeforeCreate_(c context.Context, db *gorm.DB) (*gorm.DB, error) {
	s.UUID = uuid.NewV4()
	var count int64
	err := db.Model(&UserORM{}).Where("username = ?", s.Username).Count(&count).Error
	if err != nil {
		return nil, err
	}
	if count > 0 {
		return nil, errors.New("用户名已存在")
	}
	return db, err
}

// 删除前判断 把 关联 的也删除了
func (s *UserORM) BeforeDelete_(c context.Context, db *gorm.DB) (*gorm.DB, error) {
	color.Blue("删除钩子 ")
	err := db.Where("user_id = ?", s.Id).Delete(&UserRltAuthorityORM{}).Error
	return db, err
}
